import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;

public class SocketFile {
    public static class GreetingServer extends Thread {
        private ServerSocket serverSocket;

        public GreetingServer(int port) throws IOException {
            serverSocket = new ServerSocket(port);
            serverSocket.setSoTimeout(20000);
        }

        public void run() {
            Socket server = null;
            try {
                server = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
                try {
                    System.out.println("等待客户端就绪");
                    FileInputStream fin = new FileInputStream("C:\\Users\\Administrator\\Desktop\\111.txt");
                    FileOutputStream fout = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\333.txt");
                    DataOutputStream out = new DataOutputStream(server.getOutputStream());
                    out.writeInt(fin.available());
                    DataInputStream in = new DataInputStream(server.getInputStream());
                    in.read();
                    byte[] by = new byte[fin.available()];
                    fin.read(by);
                    System.out.println("服务端开始发送数据");
                    out.write(by);
                    by = new byte[fin.available()];
                    in.read(by);
                    fout.write(by);
                    System.out.println("服务端接收完毕");
                }
                catch(IOException e) {
                    e.printStackTrace();
                }
        }
    }
    public static class Client implements Runnable{
        private Socket socket;
        private int port;
        public Client(int port) throws IOException {
            socket = new Socket("localhost", port);
            this.port=port;
        }
        public void run(){
                try {
                    DataInputStream in = new DataInputStream(socket.getInputStream());
                    int i = in.readInt();
                    FileOutputStream fout = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\222.txt");
                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                    byte[] by = new byte[i];
                    System.out.println("客户端准备完毕");
                    out.writeUTF("");
                    in.read(by);
                    fout.write(by);
                    System.out.println("客户端接收完毕");
                    BufferedReader fin = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\222.txt"));
                    String str="";
                    while (true){
                        String s;
                        if ((s=fin.readLine())!=null){
                            str+=s.toUpperCase();
                        }
                        else{
                            break;
                        }
                    }
                    by=str.getBytes();
                    System.out.println("客户端开始发送数据");
                    out.write(by);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    public static void main(String [] args) throws IOException {
        Thread t1 = new GreetingServer(8989);
        t1.start();
        Thread t2=new Thread(new Client(8989));
        t2.start();
    }
}
